package com.fzt.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzt.reggie.common.R;
import com.fzt.reggie.entity.Category;
import com.fzt.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
@Slf4j
public class CategoryController {


    @Autowired
    private CategoryService categoryService;

    /*
    新增菜品以及套餐
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("新增成功");
    }

    /*
     分页查询
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize){
        //分页构造器对象
        Page<Category> pageInfo = new Page<>(page,pageSize);

        //设置查询条件
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);

        //执行查询
        categoryService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /*
    删除分类
     */
    @DeleteMapping()
    public R<String> deleteById( Long ids){
        log.info("删除分类，id={}",ids);

        //categoryService.removeById(ids);
        categoryService.remove(ids);
        return R.success("删除成功");
    }

    /*
    修改分类信息
     */
    @PutMapping
    public R<String> updateById(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /*
        菜品分类查询
    */
    @GetMapping("/list")
    public R<List<Category>> retList( Category category){
        //根据id查询菜品类别
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //查找条件
        queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        //排序条件
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = categoryService.list(queryWrapper);

        return R.success(list);
    }


}
